package main

import (
	"flag"
	"github.com/micro/go-micro/v2"
	log "github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-plugins/wrapper/select/roundrobin/v2"
	"micro-core/config"
	nacos "micro-core/registry"
	"rpc-service/product/application/handler"
	"rpc-service/product/config/global"
	"rpc-service/product/datasource"
)

func InitServer() {
	//解析命令运行参数
	flag.Parse()
	//从config.json加载配置信息
	log.Info("Config ResourcesPath:", *config.ResourcesPath)
	log.Info("Config Env:", *config.Env)
	//从配置中心获取业务配置
	config.LoadAppConfig(global.AppConfig)
	//连接数据库
	datasource.InitDatabase()
}

func main() {
	InitServer()
	conf := global.AppConfig
	//注册中心
	registry := nacos.NewRegistry(registry.Addrs(conf.RegistryAddr))
	//负载均衡策略:轮循
	wrapper := roundrobin.NewClientWrapper()
	opts := []micro.Option{
		micro.Address(":" + conf.Port),
		micro.Name(conf.ServiceName),
		micro.Version(conf.Version),
		micro.Registry(registry),
		micro.WrapClient(wrapper),
	}

	service := micro.NewService(opts...)

	//_ = bootstrap.NewMicroService(bootstrap.Service(service)).Start()

	service.Init()
	//注册grpc handler
	handler.RegisterServiceHandlers(service.Server())
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
